<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.ParseException" %>
<%@page import="java.util.ArrayList"%>
<%@page import="com.java.FamTree.*" %>
<%@ include file="loginDetails.jsp" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<HTML>
<BODY>

<%!
String eventId = null;
String h = null;
String w = null;

boolean removeChild(JspWriter out,HttpServletRequest request,
					HttpSession session)
{
	eventId = request.getParameter("EventId");
	h = request.getParameter("H");
	w = request.getParameter("W");
	if(eventId == null || w == null || h == null)
		return false;
	// Should make a database connection here and check password
	try { // Load driver class
		Class.forName("com.mysql.jdbc.Driver");
	}
	catch (java.lang.ClassNotFoundException e) {
		System.err.println("ClassNotFoundException: " +e);
	}
	int insertId = -1;
	Connection con2 = null;
		try { 
		    con2 = DriverManager.getConnection(url+db, uid, pwd);
		    PreparedStatement newDiv = con2.prepareStatement("INSERT INTO Event (EventTypeId, EventDate, EventPlace) VALUES (2, CURRENT_DATE(), 'None')", PreparedStatement.RETURN_GENERATED_KEYS);
			
                    
                    if (newDiv.executeUpdate() > 0) {
                        ResultSet rst = newDiv.getGeneratedKeys();
                        rst.next();
                        insertId = rst.getInt(1);
                    }
		    
			
			
		} catch (SQLException ex) { 
			System.out.println(ex); 
		    System.err.println(ex); 
		}
                
		if(insertId > 0){ 
			try { 
		    	PreparedStatement update = con2.prepareStatement("Insert INTO Relationship (Husband, Wife, RelationshipTypeId, EventId) VALUES(?,?,3,?)");
			
				update.setInt(1, Integer.parseInt(h));
				update.setInt(2, Integer.parseInt(w));
				update.setInt(3, insertId);
				update.executeUpdate();
				
			} catch (SQLException ex) { 
				System.out.println(ex); 
			    System.err.println(ex); 
			} finally {
			    if (con2 != null ) {
			        try {
			            con2.close();
			        } catch (SQLException ex) {
			            System.err.println(ex);
			            return false;
			        }
			    }
			}
		}
	
	return true;
} 
%>


<%
	//Check to see if we're logged in.
	if (session.getAttribute("UserId") == null) {
	    response.sendRedirect("index.jsp"); // Send them away if they're not logged in.
	}


	boolean success = false;
	session = request.getSession(true);// May create new session
	//try{
		success = removeChild(out,request,session);
	//}
	//catch(IOException e){ System.err.println(e); }
	
	if(!success)
		response.sendRedirect("Events.jsp?"); // Success
	else
		response.sendRedirect("Events.jsp?"); // Failed login
	// Redirect back to login page with a message
%>

</BODY>
</HTML>